package cn.oowoo.api.controller;

import cn.oowoo.common.dto.BlockInfoItem;
import cn.oowoo.common.dto.Employee;
import cn.oowoo.mapper.BlockInfoItemDao;
import cn.oowoo.mapper.EmployeeRepository;
import cn.oowoo.mapper.FilmDao;
import cn.oowoo.common.dto.FilmEntity;
import com.google.gson.Gson;
import org.elasticsearch.index.query.DisMaxQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/es")
public class ElasticSearchController {

    @Autowired
    private EmployeeRepository employeeRepository;
    @Autowired
    private FilmDao filmDao;
    @Autowired
    private BlockInfoItemDao blockInfoItemDao;

    //增加
    @RequestMapping("/add/{id}")
    public String add(@PathVariable("id")String id){

        Employee employee=new Employee();
        employee.setId(id);
        employee.setFirstName("马丁");
        employee.setLastName("路德金");
        employee.setAge(26);
        employee.setAbout("");
        employeeRepository.save(employee);
        System.err.println("add a obj");

        return "success";
    }

    //批量增加
    @RequestMapping("/addall")
    public String addAll(){
        List<Employee> list = new ArrayList<>();

        Employee employee=new Employee();
        employee.setId("2");
        employee.setFirstName("SpringBoot");
        employee.setLastName("JAVA");
        employee.setAge(26);
        employee.setAbout("");
        list.add(employee);


        Employee employee1=new Employee();
        employee1.setId("3");
        employee1.setFirstName("SpringCloud");
        employee1.setLastName("Think in java");
        employee1.setAge(26);
        employee1.setAbout("");
        list.add(employee1);

        Employee employee2=new Employee();
        employee2.setId("4");
        employee2.setFirstName("Spring");
        employee2.setLastName("think in php");
        employee2.setAge(26);
        employee2.setAbout("");
        list.add(employee2);

        Employee employee3=new Employee();
        employee3.setId("5");
        employee3.setFirstName("张");
        employee3.setLastName("斌");
        employee3.setAge(26);
        employee3.setAbout("");
        list.add(employee3);

        Employee employee4=new Employee();
        employee4.setId("6");
        employee4.setFirstName("张");
        employee4.setLastName("斌");
        employee4.setFullName("张斌");
        employee4.setAge(26);
        employee4.setAbout("");
        list.add(employee4);



        employeeRepository.saveAll(list);
        System.err.println("add all obj");

        return "success";
    }


    //删除
    @RequestMapping("/delete")
    public String delete(){
        Employee employee=new Employee();
        employee.setId("6");
        employeeRepository.delete(employee);

        return "success";
    }

    //局部更新
    @RequestMapping("/update")
    public String update(){

        Employee employee=employeeRepository.queryEmployeeById("1");
        employee.setFirstName("哈哈");
        employeeRepository.save(employee);

        System.err.println("update a obj");

        return "success";
    }

    //查询
    @RequestMapping("/query/{id}")
    public Employee query(@PathVariable("id")String id){

        Employee accountInfo=employeeRepository.queryEmployeeById(id);
        System.err.println(new Gson().toJson(accountInfo));

        return accountInfo;
    }

    @RequestMapping("/query/words")
    public List<Employee> querybyFirstName(String words){
        List<Employee> accountInfo=employeeRepository.queryEmployeeByFirstName(words);
        System.err.println(new Gson().toJson(accountInfo));

        return accountInfo;
    }




    /**
     * matchQuery       : 单个字段查询
     * matchAllQuery    : 匹配所有
     * multiMatchQuery  : 多个字段匹配某一个值
     * wildcardQuery    : 模糊查询
     * boost            : 设置权重,数值越大权重越大
     * 混合搜索
     * @param content
     * @return
     */
    @RequestMapping("/query/querySearch")
    public Page<Employee> querySearch(String content){
        DisMaxQueryBuilder disMaxQueryBuilder = QueryBuilders.disMaxQuery();
        QueryBuilder ikTypeQuery = QueryBuilders.matchQuery("fullName", content).boost(2f);
        QueryBuilder pinyinTypeQuery = QueryBuilders.matchQuery("fullName.pinyin", content);
        //QueryBuilder ikCodeQuery = QueryBuilders.matchQuery("lastName", content).boost(2f);
        //QueryBuilder pinyinCodeQuery = QueryBuilders.matchQuery("lastName.pinyin", content);
        //QueryBuilder wildcardCodeQuery = QueryBuilders.wildcardQuery("lastName", content);
        //QueryBuilder multiCodeQuery = QueryBuilders.multiMatchQuery(content,"firstName","lastName");
        disMaxQueryBuilder.add(ikTypeQuery);
        disMaxQueryBuilder.add(pinyinTypeQuery);
        //disMaxQueryBuilder.add(ikCodeQuery);
        //disMaxQueryBuilder.add(pinyinCodeQuery);
        //disMaxQueryBuilder.add(wildcardCodeQuery);
        //disMaxQueryBuilder.add(multiCodeQuery);
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(disMaxQueryBuilder).build();
        Page<Employee> search = employeeRepository.search(searchQuery);
        return search;
    }







    /**
     * 拼接搜索条件
     *
     * @param name     the name
     * @param director the director
     * @return list
     */
    @RequestMapping("filmEntity/search")
    public List<FilmEntity> search(String name) {
        //使用中文拼音混合搜索，取分数最高的，具体评分规则可参照：
        //  https://blog.csdn.net/paditang/article/details/79098830
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(structureQuery(name))
                .build();
        List<FilmEntity> list = filmDao.search(searchQuery).getContent();
        return list;
    }

    /**
     * 中文、拼音混合搜索
     *
     * @param content the content
     * @return dis max query builder
     */
    public DisMaxQueryBuilder structureQuery(String content) {
        //使用dis_max直接取多个query中，分数最高的那一个query的分数即可
        DisMaxQueryBuilder disMaxQueryBuilder = QueryBuilders.disMaxQuery();
        //boost 设置权重,只搜索匹配name和disrector字段
        QueryBuilder ikNameQuery = QueryBuilders.matchQuery("name", content).boost(2f);
        QueryBuilder pinyinNameQuery = QueryBuilders.matchQuery("name.pinyin", content);
        QueryBuilder ikDirectorQuery = QueryBuilders.matchQuery("director", content).boost(2f);
        disMaxQueryBuilder.add(ikNameQuery);
        disMaxQueryBuilder.add(pinyinNameQuery);
        disMaxQueryBuilder.add(ikDirectorQuery);
        return disMaxQueryBuilder;
    }

    //增加
    @RequestMapping("/filmEntity/add")
    public String add(FilmEntity filmEntity){
        filmDao.save(filmEntity);
        return "success";
    }


    @RequestMapping("blockInfoItem/add")
    public String addBlockInfoItem(BlockInfoItem blockInfoItem){
        blockInfoItemDao.save(blockInfoItem);
        return "SUCCESS";
    }

    @RequestMapping("blockInfoItem/search")
    public Page<BlockInfoItem> searchBlock(String blockName){
        DisMaxQueryBuilder disMaxQueryBuilder = QueryBuilders.disMaxQuery();
        QueryBuilder ikTypeQuery = QueryBuilders.matchQuery("blockName", blockName).boost(2f);
        QueryBuilder pinyinTypeQuery = QueryBuilders.matchQuery("blockName.pinyin", blockName);
        //QueryBuilder ikCodeQuery = QueryBuilders.matchQuery("lastName", content).boost(2f);
        //QueryBuilder pinyinCodeQuery = QueryBuilders.matchQuery("lastName.pinyin", content);
        //QueryBuilder wildcardCodeQuery = QueryBuilders.wildcardQuery("lastName", content);
        //QueryBuilder multiCodeQuery = QueryBuilders.multiMatchQuery(content,"firstName","lastName");
        disMaxQueryBuilder.add(ikTypeQuery);
        disMaxQueryBuilder.add(pinyinTypeQuery);
        //disMaxQueryBuilder.add(ikCodeQuery);
        //disMaxQueryBuilder.add(pinyinCodeQuery);
        //disMaxQueryBuilder.add(wildcardCodeQuery);
        //disMaxQueryBuilder.add(multiCodeQuery);
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(disMaxQueryBuilder).build();
        Page<BlockInfoItem> search = blockInfoItemDao.search(searchQuery);
        return search;
    }


}